﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;
namespace BanVeXemPhim_Presentation
{
    public class NhanVienDAO:DataProvider
    {
        public int KiemTraDangNhap(string username,string pass)
        {            
            connect();
            int result = 0;
            object obj = new object();
            this.command = this.connection.CreateCommand();
            this.command.CommandText = "select nhomnguoidung from NhanVien where username=@username and password=@pass";
            SqlParameter parameter = command.Parameters.Add("@username", System.Data.SqlDbType.NVarChar);
            parameter.Value = username;

            parameter = command.Parameters.Add("@pass", SqlDbType.NVarChar);
            parameter.Value = pass;
            

            obj=this.command.ExecuteScalar();
            if(obj ==null)
            {
                result = 0;
            }
            else
            {
                result = (int)obj;
            }
           
            return result;
        }

        public NhanVienDTO[] LayDanhSachNhanVien()
        {
            connect();
            string query = "select * from nhanvien";
            adapter = new SqlDataAdapter(query, connection);
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet);
            DataTable dataTable = dataSet.Tables[0];
            int n = dataTable.Rows.Count;
            NhanVienDTO[] nhanVienDtos=new NhanVienDTO[n];
            for (int i = 0; i < n; i++)
            {
                nhanVienDtos[i] = (NhanVienDTO)GetDataFromDataRow(dataTable, i);
            }
            return nhanVienDtos;
        }
        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            NhanVienDTO nhanVienDto = new NhanVienDTO();
            try
            {
                nhanVienDto.IdNhanVien = dt.Rows[i]["IdNhanVien"].ToString();
                nhanVienDto.HoTen = dt.Rows[i]["HoTen"].ToString();
                nhanVienDto.CMND = dt.Rows[i]["CMND"].ToString();
               // string s = dt.Rows[i]["NgaySinh"].ToString();
                nhanVienDto.DiaChi = dt.Rows[i]["DiaChi"].ToString();
                nhanVienDto.NhomNguoiDung = Convert.ToInt32(dt.Rows[i]["NhomNguoiDung"].ToString());
                nhanVienDto.Username = dt.Rows[i]["Username"].ToString();
                nhanVienDto.Password = dt.Rows[i]["Password"].ToString();
                nhanVienDto.NgaySinh = Convert.ToDateTime(dt.Rows[i]["NgaySinh"].ToString());
              
            }
            catch (System.Exception ex)
            {
                string time = dt.Rows[i]["NgaySinh"].ToString();
                time = ChangeTime(time);
                nhanVienDto.NgaySinh = Convert.ToDateTime(time);
              //  MessageBox.Show(nhanVienDto.HoTen+"\r"+ex.ToString());
            }
            return nhanVienDto;
        }
        private string ChangeTime(string time)
        {
            char[] mang = time.ToCharArray();
            if (mang[time.Length - 2] == 'S' || mang[time.Length - 2] == 'C')
            {
                time = time.Replace("SA", "AM");
                time = time.Replace("CH", "PM");
            }
            else
            {
                time = time.Replace("AM", "SA");
                time = time.Replace("PM", "CH");
            }
            return time;
        }
        public void ThemNhanVien(NhanVienDTO nhanVienDto)
        {
            connect();
            string query = "insert into NhanVien(HoTen,CMND,NgaySinh,DiaChi,NhomNguoiDung,Username,Password) values(N'"+nhanVienDto.HoTen+"','"+nhanVienDto.CMND+"','"+nhanVienDto.NgaySinh.ToShortDateString()+"',N'"+nhanVienDto.DiaChi+"',"+nhanVienDto.NhomNguoiDung.ToString()+",N'"+nhanVienDto.Username+"',N'"+nhanVienDto.Password+"')";
            command = connection.CreateCommand();
            command.CommandText = query;
            command.ExecuteNonQuery();
        }

        public void XoaNhanVien(NhanVienDTO nhanVienDto)
        {
            connect();
            string query = "delete from nhanvien where Username='"+nhanVienDto.Username+"'";
            command = connection.CreateCommand();
            command.CommandText = query;
            command.ExecuteNonQuery();
        }
        public void UpdateNhanVien(NhanVienDTO nv)
        {
            connect();
            string query = "update nhanvien set HoTen=N'" + nv.HoTen + "',CMND='" + nv.CMND + "',NgaySinh='" + nv.NgaySinh.ToString() + "',DiaChi=N'" + nv.DiaChi + "',NhomNguoiDung=" + nv.NhomNguoiDung.ToString() + ",Password=N'" + nv.Password + "' where Username=N'" + nv.Username + "'";
            command = connection.CreateCommand();
            command.CommandText = query;
            command.ExecuteNonQuery();
        }

    }

}
